#include "commclass.h"
#include "commmsg.h"
#include "CTMySQL.h"
#include "CTCodeMySQL.h"
#include "CTArray.h"
#include "CTConfig.h"

using namespace CTLib;

const char* APP_CONFIG_FILE_PATH = "../config/appmysql.conf";
const char* DBSVR_CONFIG_FILE_PATH = "../config/dbsvr.conf";
short g_pet_level = 1;

class CDBConfig
{
public:
    CTSizeString<32> szDBHost;
    CTSizeString<32> szDBUser;
    CTSizeString<32> szDBPass;
    int  _iDBPort;
    CTSizeString<128> szDBSocket;
    CTSizeString<64> szDBName; 
};

void LoadConfig(CDBConfig& stConfig)
{
    const char* SECTION = "Global";
    string s;
    CTConfig cfg;
    int iRet = cfg.LoadConfig(APP_CONFIG_FILE_PATH);
    if (iRet)
    {
        printf("load config %s error\n", APP_CONFIG_FILE_PATH);
        exit(-1);
    }

    s = cfg.GetString(SECTION, "DBHost");
    stConfig.szDBHost.Set(s.c_str());

    s = cfg.GetString(SECTION, "DBUser");
    stConfig.szDBUser.Set(s.c_str());

    s = cfg.GetString(SECTION, "DBPass");
    stConfig.szDBPass.Set(s.c_str());

    s = cfg.GetString(SECTION, "DBSocket");
    stConfig.szDBSocket.Set(s.c_str());

    stConfig._iDBPort = cfg.GetInt(SECTION, "DBPort");

    //////////////////////////////////////////////////////////////////////////
    cfg.Clear();
    iRet = cfg.LoadConfig(DBSVR_CONFIG_FILE_PATH);
    if (iRet)
    {
        printf("load config %s error\n", DBSVR_CONFIG_FILE_PATH);
        exit(-1);
    }

    s = cfg.GetString(SECTION, "DBServer");
    stConfig.szDBName.Set(s.c_str());
}

void GetPetUserGID(CTMySQL& stMysql)
{
    //获取用户总数
    CReqSQL stSql;
    CTCodeMySQLResult stResult;
    char sBuf[65535];
    CTCodeStream s;
    s.Attach(sBuf, sizeof(sBuf));

    stSql.Set("select count(*) from TPlayerBaseInfo");
    stMysql.Query(stSql.GetSqlBuffer());
    MySQLEncode(s, stMysql, MYSQL_SELECT);

    int64 i64Count = 0;
    s.m_ushPos = 0;
    CodeConvert(s, stResult, NULL, bin_decode());
    if (stResult.m_iResult != 0)
    {
        printf("Exec Query ERROR: %s\n", stSql.GetSqlBuffer());
        exit(-1);
    }
    CodeConvert(s, i64Count, NULL, mysql_decode());

    //遍历
    int iFrom = 0;
    while (iFrom < (int)i64Count)
    {
        s.Attach(sBuf, sizeof(sBuf));
        stSql.Set("select Data from TPlayerBaseInfo limit %d, 5", iFrom);
        stMysql.Query(stSql.GetSqlBuffer());
        MySQLEncode(s, stMysql, MYSQL_SELECT);

        CPlayerBaseInfo stBaseInfo;
        s.m_ushPos = 0;
        CodeConvert(s, stResult, NULL, bin_decode());
        if (stResult.m_iResult != 0)
        {
            printf("Exec Query ERROR: %s\n", stSql.GetSqlBuffer());
            exit(-1);
        }

        for (int i = 0; i < stResult.m_iRowCount; ++i)
        {
            CodeConvert(s, stBaseInfo, NULL, mysql_decode());

            //计算优先级
            CPetData & stUserPet = stBaseInfo._stUserPetData;
            if (stUserPet._iPetLevel > g_pet_level)
            {
                std::cout << "GID: " << stBaseInfo._uiGID << "  PetLevel: " << stUserPet._iPetLevel << std::endl;
            }
        }

        iFrom += stResult.m_iRowCount;
    }
}

int main(int argc, char** argv)
{
    try
    {
        if (argc > 1)
        {
            g_pet_level = CT_MAX(atoi(argv[1]), g_pet_level);
        }

        //获取配置
        CDBConfig stConfig;
        LoadConfig(stConfig);

        //初始化mysql连接
        CTMySQL stMysql;
        stMysql.Connect(stConfig.szDBHost(), stConfig.szDBUser(), stConfig.szDBPass(), 
                (unsigned short)stConfig._iDBPort, stConfig.szDBSocket());
        stMysql.SelectDB(stConfig.szDBName());
                
        GetPetUserGID(stMysql);
    }
    catch (CTException& e)
    {
        printf("except: %s\n", e.GetErrorMsg());
        return -1;
    }

    return 0;
}

